﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Threading;
using System.Diagnostics;

namespace Bohdan.Arch.Messaging
{
    public abstract class GeneralMultiThreadedSubscriber : GeneralSubscriber
    {
        protected List<Thread> _lWorkers = new List<Thread>();

        public abstract int NumberOfWorkerThreads { get; }

        public override void StartWorkersSync()
        {
            _lWorkers.Capacity = NumberOfWorkerThreads;

            for (int i = 0, n = NumberOfWorkerThreads; i < n; ++i)
            {
                Thread _worker = new Thread(new ThreadStart(WorkerThread));
                _worker.Priority = WorkerThreadPriority;
                _worker.Name = this.GetType().Name;
                try
                {
                    _worker.Start();
                    _lWorkers.Add(_worker);
                }
                catch
                {
                    _worker = null;
                    throw;                      // TODO: handle
                }
            }
        }
        public override void EndWorkersSync()
        {
            foreach (var _worker in _lWorkers)
                _worker.Join();
        }

    }
}
